$(function(){
    var newsAdd = new NewsAdd();
    newsAdd.init().init();
});
    
    
function NewsAdd(){
    this.init = function(){
        return NEWSADD;
    };
    var NEWSADD = {
        init : function(){
            NEWSADD.funs.initUpload();
            NEWSADD.listeners.bindEvents();
        },
        listeners : {
            bindFormSubmit : function(){
                $("#btn_notice_add_submit").click(function(){
                    var $form = $("#news_app");
                    
                    var icon = $.trim($("#hid_news_icon").val());
                    if('' == icon){
                        alert('未上APP传缩略图');
                        
                        return;
                    }
                    
                    $form.validate();
                    
                    $form.submit();
                });
            },
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindUpload : function(){
                $('#btn_app_icon_upload_enter').click(function(){
                    $('#btn_app_icon_upload').uploadifyUpload();
                });
            }
        },
        funs  : {
            initUpload : function(){
                $('#btn_app_icon_upload').uploadify({
                    'uploader'  : SYSTEM_DATA.public_path + '/js/plugin/Uploadify/uploadify.swf',
                    'script'    : SYSTEM_DATA.contextPath + '/File/upload',
                    'cancelImg' : SYSTEM_DATA.public_path + '/js/plugin/Uploadify/cancel.png',
                    'folder'    : '/uploads',
                    'removeCompleted' : true,
                    'auto'       : false,
                    'multi': false,
                    'fileExt' : '*.jpg;*.jpeg;*.png;*.gif',
                    'fileDesc' : '可上传的图片文件',
                    'sizeLimit' : 1024 * 1024 * 30,
                    //'buttonText' : '上传',
                    'onComplete'  : function(event, ID, fileObj, response, data) {
                        //console.debug(response);
                        //console.debug(data);
                        response = $.parseJSON(response);
                        //var lastId = response.data;
                        //                        console.debug(response);
                        //$fileId = $('#hid_file_id');
                        //alert(response.data.path);
                        $("#hid_news_icon").val(response.data.thumb_url);
                    //$fileId.val('' == $fileId.val() ? lastId : $fileId.val() + ',' + lastId);
                    },
                    'onError' : function(event,ID,fileObj,errorObj) {
                    //                        console.debug(errorObj);
                    }
                });
            },
            
            
            //设置组件是否禁用
            setElementEnabled : function($element, enabled){
                if(enabled){
                    $element.removeAttr('disabled');
                }else{
                    $element.attr('disabled', 'disabled');
                }
            },
            
            /*
             *修改App信息时点类型弹出显示已选中的类型
             **/
            initTypeId : function($dialog, typeId){
                var $chk = $dialog.find(':checkbox');
                $.each($chk, function(index, checkbox){
                    if(-1 != $.inArray($(checkbox).val(), typeId)){//alert($(checkbox).val());
                        $(checkbox).attr('checked', 'checked');
                    }
                });
            }
        }
    };
}